import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View,Button,Image} from 'react-native';
import Sqlite from 'react-native-sqlite-storage'
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import * as officeActions from './actions/officeActions';

import ModelCatalogCategory from '../../model/catalog/category';
import ModelCatalogProduct from '../../model/catalog/product';
import ModelUser from '../../model/user';
import ModelVersion from '../../model/version';

import downloadFiles, { downloadFile } from '../../utils/downImgUtils';
import testdata from '../../access/test.json';

class Office extends Component {
      constructor(props){
            super(props);
            this.state = {
                  image: null
            }
      }

      componentWillMount(){
            console.log(this.props);
      }
      
      render() {
            let {office} = this.props;
            return (
                  <View style={styles.container}>
                        <Text>{office.num}</Text>
                        <Text>office jsx</Text>
                        <Button onPress = {()=>this.testApi()} title = "testApi"></Button>
                        <Button onPress = {()=>this.testAction()} title = "testAction"></Button>
                        <Button onPress = {()=>this.testModel()} title = "testModel"></Button>
                        {
                              this.state.image ? (
                                    <Image style = {{width:100,height:100}} source={{uri: this.state.image}} />
                              ) : null
                        }
                  </View>
            );
      }

      testApi(){
            this.$http.post("menua", {test:1})
            .then((rdata)=>{
                  console.log(rdata);
            })
            .catch(e => {
                  console.log(e);
            });
      }
      
      testAction(){
            this.props.changeNum();
      }

      async testModel() {
            /*
            let category = new ModelCatalogCategory();
            let data = {
                  name: "category1111111",
                  name2: null,
                  sort_order: 0
            };

            //console.log(category.toString(params));
            //console.log(category.escape("Hello Tony's Wife"));
            //console.log(category.escape('Hello Tony"s Wife'));
            //console.log(category.insert("test", {aaa: "asasas", dddd: "aadsadad"}));
            
            let rowsAffected = await category.save(data, 2);
            console.log("row affected >>>>> ", rowsAffected);

            //let result = await category.remove(1);
            //console.log("delete result >>>>> ", result);
            console.log("count of category >>>>> ", await category.count());

            let rows = await category.query(0, 10);
            for (let key in rows) {
                  console.log("row >>>>>>>> ", rows[key]);
            }
            */

            /*
            let product = new ModelCatalogProduct();
            //let ids = await product.getCategories(2);
            let ids = null;
            if (Array.isArray(ids)) {
                 console.log("call me onn line# 57");
            } else {
                  console.log("call me onn line# 59");
            }
            //console.log("categories >>>> ", typeof(ids));
            */

            /*
            let a = [1, 2, 3, 4, 5, 6];
            let b = [5, 6, 7, 8, 9];

            let c1 = a.filter(el => !b.includes(el));
            let c2 = b.filter(el => !a.includes(el));

            for (i of a) {
                  console.log("i >>>> ", i);
            }

            //console.log("c1 >>>> ", c1);
            //console.log("c2 >>>> ", c2);

            //let product = new ModelCatalogProduct();
            //await product.saveCategories(2, [1, 2, 3, 4]);
            let test = {a: "hello", b: "ddddd"};
            console.log("test1 >>>> ", test);
            let b = test.b;
            delete test.b;
            console.log("test2 >>>> ", test);
            console.log("test2 b >>>> ", b);
            */

            let modelCategory = new ModelCatalogCategory();
            let modelProduct = new ModelCatalogProduct();
            let modelVersion = new ModelVersion();
            let modelUser = new ModelUser();


            //await modelCategory.clean();
            //await modelProduct.clean();

            /*
            await modelCategory.clean();
            await modelProduct.clean();
            
            let data = {
                  name: "category2",
                  name2: null,
                  description: "test test test",
                  sort_order: 0
            };
            let categoryId = await modelCategory.save(data);
            console.log("category >>>> ", categoryId);
            */
            
            /*
            let data = {
                  qty: 100,
                  status: 1,
                  price: 12.99,
                  special_price: 8.99,
                  points: 100,
                  name: 'product1111 in english',
                  name2: 'product1111 in chinese',
                  description: 'test test test',
                  sort_order: 0,
                  version: 100,
                  images: [
                        {
                              name: '3/4/test.jpg',
                              sort_order: 0,
                              version: 100,
                        }
                  ],
                  options: [{
                              name: 'option1111222222',
                              required: 0,
                              sort_order: 0,
                              values: [{
                                          name: 'value11222',
                                          price_prefix: '+',
                                          price: 1.00,
                                          points_prefix: '-',
                                          points: 50,
                                          sort_order: 0
                                    },
                                    {
                                          name: 'value2122',
                                          price_prefix: '+',
                                          price: 1.20,
                                          points_prefix: '-',
                                          points: 100,
                                          sort_order: 1
                                    }
                              ]
                  }],
                  categories: [2]
            };
            let productId = await modelProduct.saveProduct(data, 7);
            console.log("product# >>>>> ", productId);
            */

            /*
            let products = await modelProduct.getProducts();
            console.log(">>>>> ", products);
            let product = await modelProduct.getProduct(6);
            console.log(">>>>> ", product);
            */

            //let categories = await modelCategory.getCategoryProducts();
            //console.log(">>>>", categories);

            /*
            let now = (new Date()).format("yyyy-MM-dd HH:mm:ss");
            console.log("now >>>>> ", now);
            */

            /*
            let data = {
                  user_group: [
                        {
                              name: "Group1",
                              permission: JSON.stringify(["hello", "world", "test", "haha"]),
                              server_group_id: 1,
                              version: 2
                        }
                  ],
                  user: [
                        {
                              master_flag: 1,
                              status: 1,
                              logo: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535953349964&di=05e51aa00a7f1bc20038e3ee56557185&imgtype=0&src=http%3A%2F%2Fimg02.tooopen.com%2Fimages%2F20151217%2Ftooopen_sy_151850658381.jpg",
                              first_name: "Tony",
                              last_name: "Lee",
                              pincode: "123456",
                              server_user_id: 1,
                              version: 1,
                              groups: [1]
                        }
                  ],
                  category: [
                        {
                              name: "category1",
                              name2: "category1",
                              image: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535953349964&di=05e51aa00a7f1bc20038e3ee56557185&imgtype=0&src=http%3A%2F%2Fimg02.tooopen.com%2Fimages%2F20151217%2Ftooopen_sy_151850658381.jpg",
                              description: "test test test",
                              sort_order: 0,
                              server_category_id: 100,
                              version: 100
                        }
                  ],
                  product: [
                        {
                              server_product_id: 500,
                              qty: 100,
                              status: 1,
                              price: 12.99,
                              special_price: 8.99,
                              points: 100,
                              name: 'product2222 in english',
                              name2: 'product2222 in chinese',
                              description: 'test test test',
                              sort_order: 0,
                              version: 100,
                              images: [
                                    {
                                          server_image_id: 10,
                                          name: "test.jpg",
                                          url: "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535953349964&di=05e51aa00a7f1bc20038e3ee56557185&imgtype=0&src=http%3A%2F%2Fimg02.tooopen.com%2Fimages%2F20151217%2Ftooopen_sy_151850658381.jpg",
                                          sort_order: 0,
                                          version: 100,
                                    }
                              ],
                              options: [
                                    {
                                          server_option_id: 200,
                                          option_id: 6,
                                          name: 'option1111222222',
                                          required: 0,
                                          sort_order: 0,
                                          values: [
                                                {
                                                      server_value_id: 201,
                                                      value_id: 11,
                                                      name: 'value11222',
                                                      price_prefix: '+',
                                                      price: 1.00,
                                                      points_prefix: '-',
                                                      points: 50,
                                                      sort_order: 0
                                                },
                                                {
                                                      server_value_id: 202,
                                                      value_id: 12,
                                                      name: 'value2122',
                                                      price_prefix: '+',
                                                      price: 1.20,
                                                      points_prefix: '-',
                                                      points: 100,
                                                      sort_order: 1
                                                }
                                          ]
                              }],
                              categories: [100]
                        }
                  ]
            };
            console.log("call me on line# >>>> 190");
            await modelVersion.download(data);
            console.log("call me on line# >>>> 193");

            let user = await modelUser.login("123456");
            console.log(user);
            */

            /*
            let perms = ['hello', 'world', 'test', 'haha'];
            console.log(JSON.stringify(perms));

            if (perms.includes('hello')) {
                  console.log("it has permission: hello");
            } else {
                  console.log("it doesn't have permission: hello");
            }

            for (p of perms) {
                  console.log("the perm item >>> ", p);
            }*/

            /*
            await downloadFile("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535953349964&di=05e51aa00a7f1bc20038e3ee56557185&imgtype=0&src=http%3A%2F%2Fimg02.tooopen.com%2Fimages%2F20151217%2Ftooopen_sy_151850658381.jpg", "a/b/d/hahaha", (path) => {
                  //console.log(path);
                  this.setState({image: path}); 
            });
            */

            /*
            await modelVersion.download(testdata, (status) => {
                 console.log("status >>> ", status);
            });
            //console.log(testdata);
            */
            
      }
}
    
export default connect(
      state => ({
            office: state.office
      }),
      dispatch => bindActionCreators(officeActions, dispatch)
)(Office);

const styles = StyleSheet.create({
      container: {
            flex: 1,
            justifyContent: 'center',
            alignItems: 'center',
            backgroundColor: '#F5FCFF',
      },
      welcome: {
            fontSize: 20,
            textAlign: 'center',
            margin: 10,
      },
      instructions: {
            textAlign: 'center',
            color: '#333333',
            marginBottom: 5,
      }
});